<?php
class DrawAction extends CommonAction {
    private $Draw;
	private $User;

	public function __construct(){
		parent::__construct();
		$this->checkAuth();
		$this->checkReal();
	    $this->Draw = D('Draw');
		$this->User = D('User');
	}
	//空操作
	Public function _empty(){
	    header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
	    $this->display("Public:404");
	}
	public function all(){
        $per_num = 10;
        $page = chkNum($_GET['page']) ? $_GET['page'] : 1;
		$count = $this->Draw->where('uid='.$this->auth['id'])->count();
        $page_num = ceil($count/$per_num);
        if($page > $page_num) $page = $page_num;

        $list=$this->Draw->where('uid='.$this->auth['id'])->order('ctime desc')->limit(($page-1)*$per_num.','.$per_num)->select();

        $this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('page_num',$page_num);
		$this->assign('mod','list');
		$this->assign('empty','<tr><td colspan="5" style="text-align:center">没有找到数据！</td></tr>');

		$this->display('./Tpl/Home/Draw_list.html');
    }

    public function index(){
        $per_num = 10;
        $page = chkNum($_GET['page']) ? $_GET['page'] : 1;
		$count = $this->Draw->where('uid='.$this->auth['id'])->lock('true')->count();
        $page_num = ceil($count/$per_num);
        if($page > $page_num) $page = $page_num;

        $list=$this->Draw->where('uid='.$this->auth['id'])->order('ctime desc')->limit(($page-1)*$per_num.','.$per_num)->lock('true')->select();

        $this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('page_num',$page_num);
		$this->assign('mod','list');
		$this->assign('empty','<tr><td colspan="5" style="text-align:center">没有找到数据！</td></tr>');

		$this->display('./Tpl/Home/Draw.html');
    }

	public function add(){
	    $list=M('Bank')->where("uid={$_SESSION['USER_KEY_ID']}")->find();
	    $this->assign('bank',$list);
	    
	    $draw=M("Draw")->where("uid = '{$_SESSION['USER_KEY_ID']}' and status = '0'")->find();
		
	
	    if(!empty($draw)){
	    	$this->assign("draw",1);
	    
	    	$draw['num']=intval($draw['num']);
	    	$this->assign("tixian",$draw);
	    }else{
	    
	    	$this->assign("draw",2);
	    }
	    //提现文章
	    $art=M("Art")->where("id='43'")->find();
	    $this->assign("art",$art);
	    
	    
	    $this->display('./Tpl/Home/Draw.html');
	}

	public function insert(){
        $num = trim($_POST['num']);
		$paypwd = md5(trim($_POST['password']));
		$code = trim($_POST['code']);
        
		if ($code!=$_SESSION['code']){
		    $data['status']=0;
		    $data['info']="验证码不正确";
		    $this->ajaxReturn($data);
		}
		if ($paypwd!=$this->auth['paypwd']){
		    $data['status']=0;
		    $data['info']="交易密码不正确";
		    $this->ajaxReturn($data);
		}
		if($num<100){
		    $data['status']=0;
		    $data['info']="提现金额不能小于100";
		    $this->ajaxReturn($data);
		}
        $mo = new Model();
		$mo->query('set autocommit=0');
		$mo->query('lock tables t_user write,t_draw write');
		$rs = array();
		$u = $mo->table('t_user')->where('id='.$this->auth['id'])->find();
		if($u['rmb']<$num){
		    $data['status']=0;
		    $data['info']="余额不足，不能提现";
		    $this->ajaxReturn($data);
		}
//个人银行的id
		$rs[] = $mo->table('t_draw')->add(array(
			'uid'=>$u['id'],
			'num'=>$num,
			'fee'=>$num*$this->sys['drawfee']/100,
			'rnum'=>$num-$num*$this->sys['drawfee']/100,
			'ctime'=>time()
		));

		$rs[] = $mo->table('t_user')->save(array(
		    'id'=>$u['id'],
			'rmb'=>array('exp','rmb-'.$num)
		));
		
		if(chkArr($rs)){
			$mo->query('commit');
		    $mo->query('unlock tables');
		     $this->ajaxReturn(array('status=1'));
		}else{
			$mo->query('rollback');
		    $this->ajaxReturn(array('status=1','info'=>"提交失败"));
		}
    }
}